import axios from '@/utils/request'

export default {
  HuifuPosDevice: {
    form: {
      fields: [
        'store',
        {
          property: 'terminalId',
          field_options: { label: '终端号' }
        },
        {
          property: 'depositType',
          field_options: { label: '类型' },
          help: '不填/选"huifu-pos"时费率为1.5%。选"0.7%"时费率为0.7%。选"interior"时费率为无费率',
          component: {
            props: ['form', 'property'],
            render(h) {
              return (
                <el-select v-model={this.form[this.property]} filterable clearable>{
                  [
                    { label: 'huifu-pos', value: 'huifu-pos' },
                    { label: '0.7%', value: '0.7%' },
                    { label: 'interior', value: 'interior' }
                  ]
                    .map(v => {
                      return <el-option key={v.value} label={v.label} value={v.value} />
                    })
                }</el-select>
              )
            }
          }
        },
        {
          property: 'targetUser',
          field_options: { label: '台号用户' },
          required: false,
          component: {
            props: ['form', 'property'],
            data() {
              return { data: [] }
            },
            created() {
              axios.get(
                `/manage/counters`, { params: { 'limit': 1000000 }}
              ).then((res) => {
                if (res.data != null) {
                  this.data = res.data
                }
              })
            },
            render(h) {
              return (
                <el-select v-model={this.form[this.property]} clearable filterable>{
                  Object.assign(this.data).map(v => {
                    return <el-option key={v.connectedDispatcher.id} label={v.name + '(' + v.store.__toString + ')'} value={v.connectedDispatcher.id} />
                  })
                }</el-select>
              )
            }
          }
        }
      ]
    },
    list: {
      list_display: [
        'id',
        'store',
        {
          property: 'terminalId',
          label: '终端号'
        },
        {
          property: 'depositType',
          label: '类型'
        },
        {
          property: 'targetUser',
          label: '用户',
          component: {
            props: ['data'],
            data() {
              return {
                message: this.data?.__toString
              }
            },
            created() {
              if (this.data) {
                this.axios.get(`/manage/counters`, { params: {
                  '@filter': `entity.getConnectedDispatcher().getId() == ${this.data.id}`
                }}).then((res) => {
                  if (res.data.length > 0) {
                    this.message = res.data[0].name + '(' + res.data[0].store.__toString + ')'
                  }
                })
                this.axios.get(`/manage/stores`, { params: {
                  '@filter': `entity.getUser().getId() == ${this.data.id}`
                }}).then((res) => {
                  if (res.data.length > 0) {
                    this.message = '场所账户(' + res.data[0].name + ')'
                  }
                })
              }
            },
            render(h) {
              return <span>{this.message}</span>
            }
          }
        }
      ]
    }
  }
}
